package ca.etsmtl.log430.lab2.gestion;
import java.util.ArrayList;
import java.util.Collection;
import ca.etsmtl.log430.lab2.gestion.ifaces.*;
import ca.etsmtl.log430.lab2.gestion.ifaces.ITeacherAssignment;

/* Modification Log
 ****************************************************************************
 *v2.0, S. Audet-Arsenault, 2012-Feb-02 - Refactor rethink lab 2. added validation and exceptions.
 *
 * v1.3, R. Champagne, 2012-Feb-02 - Various refactorings for new lab.
 * 
 * v1.2, 2011-Feb-02, R. Champagne - Various refactorings, javadoc comments.
 *  
 * v1.1, 2002-May-21, R. Champagne - Adapted for use at ETS. 
 * 
 * v1.0, 12/29/99, A.J. Lattanze - Original version.

 ****************************************************************************/
/**
 * 
 * @author Lattanze
 *
 */
public class Teacher implements ITeacher {
	
	


	private static final int TEACHER_COURSE_LIMIT = 3;

	private static final int CHARGE_COURSE_LIMIT = 5;

	/**
	 * Teacher's Last Name
	 */
	private String lastName;
	
	/**
	 * Teacher's First Name
	 */
	private String firstName;
	
	/**
	 * Teacher's identification number
	 */
	private String teacherID;
	
	/**
	 * teacher type (PRF = prof, CHR = charge de cours, e.g. T.A. in French)
	 */
	private TeacherType teacherType;

	/**
	 *  List of courses taken by the student
	 */
	private Collection<String> coursesTaughtList = new ArrayList<String>();

	/**
	 *  List of courses the student is registered for
	 */
	private Collection<ICourse> coursesAssignedList = new ArrayList<ICourse>();

	private ITeacherAssignment controller;
	/**
	 * 
	 * @param lastName
	 * @param firstName
	 * @param teacherID
	 * @param teacherType
	 */
		public Teacher(
				String lastName, 
				String firstName, 
				String teacherID,
				TeacherType teacherType,
				ITeacherAssignment controller) {
			super();
			this.lastName = lastName;
			this.firstName = firstName;
			this.teacherID = teacherID;
			this.teacherType = teacherType;
			this.controller= controller;
		}
	
		
				
	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.gestion.ITeacher#getTeacherType()
	 */
	@Override
	public TeacherType getTeacherType() {
		return teacherType;
	}

	/**
	 * 
	 * @param teacherType
	 */
	public void setTeacherType(TeacherType teacherType) {
		this.teacherType = teacherType;
	}

	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.gestion.ITeacher#getCoursesAssignedList()
	 */
	@Override
	public Collection<ICourse> getCoursesAssignedList() {
		return coursesAssignedList;
	}

	/**
	 * 
	 * @param coursesAssignedList
	 */
	public void setCoursesAssignedList(Collection<ICourse> coursesAssignedList) {
		this.coursesAssignedList = coursesAssignedList;
	}

	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.gestion.ITeacher#getNumberOfCourseAssign()
	 */
	@Override
	public int getNumberOfCourseAssign()
	{
		int numberOfCourseAssign=0;
		for (ICourse assignCourse : getCoursesAssigned()) {
			if(assignCourse.getType().equals(CourseType.CRS))
				numberOfCourseAssign++;
		}
		
		return numberOfCourseAssign;
	}
	
	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.gestion.ITeacher#assignCourse(ca.etsmtl.log430.lab2.gestion.ifaces.CourseManagementInterface)
	 */
	@Override
	public boolean assignCourse(ICourse course) throws TeacherAssignmentException 
	{
		Collection<? extends ICourse> courses = getCoursesAssigned();
		
		
		
		for (ICourse iterable_element : courses) {
			
			//VALIDATION 1
			if(course.equals(iterable_element))
				throw new TeacherAssignmentException("Course:"+course.getCourseID()+" group:"+course.getGroup()+" has been already assinged to that teacher");
			
			//VALIDATION 2
			if(iterable_element.isConflicting(course))
				throw new TeacherAssignmentException("Conflit d'horaire: between ("+course.getCourseID()+","+iterable_element.getCourseID()+")");
		}
		
		// VALIDATION FOR COURSE ONLY
		if(course.getType().equals(CourseType.CRS))
		{
			if(this.teacherType.equals(TeacherType.PRF))
			{		
				//VALIDATION 3
				if( getNumberOfCourseAssign()==TEACHER_COURSE_LIMIT)
					throw new TeacherAssignmentException("Teacher has reached his Limit of:"+TEACHER_COURSE_LIMIT);
			}
			else
			{
				//VALIDATION 4
				if(getNumberOfCourseAssign()==CHARGE_COURSE_LIMIT)
					throw new TeacherAssignmentException("Teacher has reached his Limit of:"+CHARGE_COURSE_LIMIT);
			}
		}
		getCoursesAssigned().add(course);
		
		return true;
	} // Register

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
/* (non-Javadoc)
 * @see ca.etsmtl.log430.lab2.gestion.ITeacher#getLastName()
 */
	@Override
	public String getLastName() {
		return lastName;
	}
/**
 * 
 * @param firstName
 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
/* (non-Javadoc)
 * @see ca.etsmtl.log430.lab2.gestion.ITeacher#getFirstName()
 */
	@Override
	public String getFirstName() {
		return firstName;
	}
/**
 * 
 * @param teacherID
 */
	public void setTeacherID(String teacherID) {
		this.teacherID = teacherID;
	}
/* (non-Javadoc)
 * @see ca.etsmtl.log430.lab2.gestion.ITeacher#getTeacherID()
 */
	@Override
	public String getTeacherID() {
		return teacherID;
	}
/**
 * 
 * @param coursesTaughtList
 */
	public void setCoursesTaughtList(Collection<String> coursesTaughtList) {
		this.coursesTaughtList = coursesTaughtList;
	}
/* (non-Javadoc)
 * @see ca.etsmtl.log430.lab2.gestion.ITeacher#getCoursesTaughtList()
 */
	@Override
	public Collection<String> getCoursesTaughtList() {
		return coursesTaughtList;
	}
/**
 * 
 * @param coursesAssigned
 */
	public void setCoursesAssigned(Collection<ICourse> coursesAssigned) {
		this.coursesAssignedList = coursesAssigned;
	}
/* (non-Javadoc)
 * @see ca.etsmtl.log430.lab2.gestion.ITeacher#getCoursesAssigned()
 */
	@Override
	public Collection<ICourse> getCoursesAssigned() {
		return coursesAssignedList;
	}

	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.gestion.ITeacher#addCourseToBeTaught(ca.etsmtl.log430.lab2.gestion.Course)
	 */
	@Override
	public void addCourseToBeTaught(Course course) {
		this.coursesTaughtList.add(course.getCourseID());
		
	}
	
	/* (non-Javadoc)
	 * @see ca.etsmtl.log430.lab2.gestion.ITeacher#addCourseToBeTaught(java.lang.String)
	 */
	@Override
	public void addCourseToBeTaught(String courseId) {
		this.coursesTaughtList.add(courseId);
		
	}
	
	@Override
	public String toString()
	{
		return this.teacherID+" "+this.firstName+" "+this.lastName;
	}

} // Teacher class
